﻿var orderModel=require('../models/order');
var Alipay=require('../libray/alipay');
var Utl=require('../libray/utl');
var path = require('path');
var order='';
const orderOk = {
    orderOk: (req, res, next) => {
        orderModel.findOne({user_id:req.session.loginUser}).sort({number:-1}).then(doc=>{
order=doc.info[0]
            res.render('orderOk', { order:doc.info,loginUser: req.session.loginUser })
        })
    },
    updataOrder: (req, res, next) => {
        orderModel.findOne({ user_id: req.session.loginUser }).sort({ number: -1 }).then(doc => {
            orderModel.updateOne({_id:doc._id},{money:req.body.money}).then(doc2=>{
            })
        })
    },
    orderDemo: (req, res, next)=>{
        var ali = new Alipay({
            appId: '2016091100485209',
            notifyUrl: 'http://www.haonewbee.com:5000/',
            rsaPrivate: path.resolve('./key/private_key.pem'),
            rsaPublic: path.resolve('./key/alipay_public_key.pem'),
            sandbox: true,
            signType: 'RSA2'
        });
        var url = ali.webPay({
            body: "书籍购买",
            subject: "栗小豪的书店",
            outTradeId: new Date().toString(),
            timeout: '90m',
            amount: order.money,
            sellerId: '',
            product_code: 'FAST_INSTANT_TRADE_PAY',
            goods_type: "1",
            return_url: "http://www.haonewbee.com:5000/",
        })
        var url_API = 'https://openapi.alipaydev.com/gateway.do?' + url;
        res.json({
            status:1,
            url: url_API
        });
    }
}
module.exports = orderOk
